package org.dentalcard.ui.main;

import org.dentalcard.entity.DecUser;
import org.dentalcard.service.PersistenceManager;
import org.dentalcard.service.impl.MenuManager;
import org.dentalcard.ui.calendar.DayCalendarScr;
import org.dentalcard.ui.i18n.I18nManager;
import org.dentalcard.xml.XMLMenu;
import org.dentalcard.xml.XMLMenuItem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @user: Vladimir Chernishov
 * @date: 19.12.11
 * @time: 15:49
 */
public class MainFrameBuilder {
    private static MainFrameBuilder instance = new MainFrameBuilder();

    public static MainFrameBuilder getInstance() {
        return instance;
    }

    private final JFrame mainFrame;
    private final JPanel rootPanel;
    private DecUser decUser;
    private JPanel leftPanel;
    private JDesktopPane rightPanel;
    
    private MainFrameBuilder() {
        mainFrame = new JFrame(I18nManager.getRes("dental.cards"));
        rootPanel = new JPanel(new BorderLayout());
        init();
    }


    private void init() {
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        leftPanel = new JPanel();
        rightPanel = new JDesktopPane();
        rootPanel.add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel), BorderLayout.CENTER);

        mainFrame.getContentPane().add(rootPanel);
        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                PersistenceManager.close();
            }
        });
        createMenu();
    }

    private void createMenu() {
        JMenuBar menubar = new JMenuBar();
        MenuManager menuManager = new MenuManager();
        XMLMenu menu = menuManager.getMenu();
        JMenu jmenu = new JMenu(I18nManager.getRes(menu.getName()));
        menubar.add(jmenu);
        addMenutoMenuBar(jmenu, menu);
        
        mainFrame.setJMenuBar(menubar);
    }

    private void addMenutoMenuBar(JMenu jmenu, XMLMenu menu) {
        if (menu.getItems() != null && !menu.getItems().isEmpty()) {
            for (XMLMenuItem item : menu.getItems()) {
                jmenu.add(createMenuItem(item));
            }
        }
        if (menu.getMenu() != null && !menu.getMenu().isEmpty()) {
            for (XMLMenu xmlMenu : menu.getMenu()) {
                JMenu innerMenu = new JMenu(I18nManager.getRes(xmlMenu.getName()));
                jmenu.add(innerMenu);
                addMenutoMenuBar(innerMenu, xmlMenu);
            }
        }
    }

    private JMenuItem createMenuItem(final XMLMenuItem item) {
        JMenuItem ret = new JMenuItem(I18nManager.getRes(item.getName()));
        ret.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                openScreen(item.getClaz());
            }
        });
        return ret;
    }

    public void openScreen(Class<? extends JInternalFrame> claz) {
        try {
            JInternalFrame scr = claz.newInstance();
            scr.setVisible(true);
            rightPanel.add(scr);
            scr.setSelected(true);
            scr.setMaximum(true);
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }catch (java.beans.PropertyVetoException ex) {
            ex.printStackTrace();
        }
    }


    public void show(){
        mainFrame.pack();
        mainFrame.setExtendedState(mainFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        mainFrame.setVisible(true);
    }

    public void setDecUser(DecUser decUser) {
        this.decUser = decUser;
    }

    public DecUser getDecUser() {
        return decUser;
    }

    public static JFrame getMainFrame() {
        return instance.mainFrame;
    }

    public static DecUser getUser() {
        return instance.decUser;
    }
}
